package com.example.cstr.service.serviceImpl;

import com.example.cstr.entity.*;
import com.example.cstr.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.example.cstr.service.YearService;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class YearServiceImpl implements YearService {

    @Resource
    private YearMapper yearMapper;

    @Resource
    private EducationTrainClassMapper educationTrainClassMapper;

    @Resource
    private EducationTrainProjectMapper educationTrainProjectMapper;

    @Resource
    private StudyContainMapper studyContainMapper;

    @Resource
    private FileMapper fileMapper;

    /**
     * 逻辑：
     * 删除年度--》删除培训类别--》删除培训项目--》删除学习内容--》删除文件
     */
    @Override
    public Message deleteByPrimaryKey(Integer yearId) {
        Integer i = 0;
        yearMapper.deleteByPrimaryKey(yearId);
        List<EducationTrainClass> etcs = educationTrainClassMapper.selectAllByYearId(yearId);
        for (EducationTrainClass etc : etcs) {
            Integer etcid1 = etc.getEtcId();
            educationTrainClassMapper.deleteByPrimaryKey(etcid1);
            List<EducationTrainProject> etps = educationTrainProjectMapper.selectByEtp_parent_id(etcid1);
            for (EducationTrainProject etp : etps) {
                Integer etpid1 = etp.getEtpId();
                educationTrainProjectMapper.deleteByPrimaryKey(etpid1);
                List<StudyContain> scs = studyContainMapper.selectBySc_etc_idAndSc_etp_id(etcid1, etpid1);
                for (StudyContain sc : scs) {
                    Integer scid1 = sc.getScId();
                    studyContainMapper.deleteByPrimaryKey(scid1);
                    List<File> fs = fileMapper.selectFileInfoByscId(scid1);
                    for (File f : fs) {
                        Integer fid1 = f.getFileId();
                        String fname = f.getFileName();
                        fileMapper.removeMyFileByOrdName(fname);
                        i = fileMapper.deleteByPrimaryKey(fid1);
                    }
                }
            }
        }
        return i > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(Year record) {
        return yearMapper.insert(record);
    }

    @Override
    public int insertSelective(Year record) {
        return yearMapper.insertSelective(record);
    }

    @Override
    public Year selectByPrimaryKey(Integer yearId) {
        return yearMapper.selectByPrimaryKey(yearId);
    }

    @Override
    public int updateByPrimaryKeySelective(Year record) {
        return yearMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Year record) {
        return yearMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Year> selectAllYear() {
        return yearMapper.selectAllYear();
    }

    @Override
    public Message PagingSearch(@RequestParam Integer pageNum, @RequestParam Integer size) {
        PageHelper.startPage(pageNum, size);
        List<Year> yearList = yearMapper.selectAllYear();
        PageInfo pageInfo = new PageInfo(yearList);
        pageInfo.getTotal();
        return Message.success(null).add(pageInfo);
    }

    @Override
    public Year selectCurrentYear() {
        return yearMapper.selectCurrentYear();
    }
}







